package com.vq3.digitalizationsoftwareintegrates.Servlet.DataCenter;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vq3.digitalizationsoftwareintegrates.Entity.DataCenter.DataCenterEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.DataCenter.DataCenter_permissionEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.Info.PageInfo;
import com.vq3.digitalizationsoftwareintegrates.Entity.Info.ResultInfo;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.S_C_DatesEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.S_C_PointEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.S_C_Point_ContentEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.Permission_TBEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.UserEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.User_RoleEntity;
import com.vq3.digitalizationsoftwareintegrates.Service.DataCenter.DataCenterService;
import com.vq3.digitalizationsoftwareintegrates.Service.Index.IndexSoftwareService;
import com.vq3.digitalizationsoftwareintegrates.Service.Spot_Check.Spot_Check_FusionService;
import com.vq3.digitalizationsoftwareintegrates.Utils.ImageUtil.CalculationIsType;
import com.vq3.digitalizationsoftwareintegrates.Utils.PackagedDataUtil.Impl.PackagedDataImpl;
import com.vq3.digitalizationsoftwareintegrates.Utils.PackagedDataUtil.PackagedData;
import com.vq3.digitalizationsoftwareintegrates.Utils.PermissionControl.GetUserPermission;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.*;

@RestController
@RequestMapping("dataCenter")
public class DataCenterServlet {
    @Resource
    IndexSoftwareService indexSoftwareService;
    @Resource
    DataCenterService dataCenterService;
    @Resource
    Spot_Check_FusionService spot_check_fusionService;
    @Resource
    GetUserPermission getUserPermission;



    @RequestMapping("getDataCenterSideBar")
    public void getDataCenterSideBar(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {

        ResultInfo resultInfo = new ResultInfo();
        PackagedData packagedData = new PackagedDataImpl();
        List<DataCenterEntity> dataCenterSideBar = new ArrayList<>();
        resultInfo.setFlag(false);

        UserEntity userEntity = (UserEntity)httpServletRequest.getSession().getAttribute("USER");
        List<Permission_TBEntity> permission_tbEntities = getUserPermission.GetPermission(userEntity.getUname());
        Set<Integer> didS = new HashSet<>();
        if (!permission_tbEntities.isEmpty()){
            for (Permission_TBEntity permission_tbEntity : permission_tbEntities) {
                List<DataCenter_permissionEntity> dataCenter_permissionEntities = dataCenterService.perfindDataCenterid(permission_tbEntity);
                if (!dataCenter_permissionEntities.isEmpty()) {
                    for (DataCenter_permissionEntity dataCenter_permissionEntity : dataCenter_permissionEntities) {
                        didS.add(dataCenter_permissionEntity.getDid());
                    }
                }
            }
        }
        for (Integer did : didS) {
            DataCenterEntity dataCenterEntity = dataCenterService.findDataCenterSideBar(did);
            if (dataCenterEntity != null) {
                dataCenterSideBar.add(dataCenterEntity);
            }
        }
        if (!dataCenterSideBar.isEmpty()) {
            resultInfo.setFlag(true);
            resultInfo.setData(dataCenterSideBar);
        }

        /*List<DataCenter_permissionEntity> dataCenter_permissionEntities = dataCenterService.perfindDataCenterid(permission_tbEntities);*/





        packagedData.writeValue(resultInfo,httpServletResponse);


    }

    @RequestMapping("getS_CData")
    public void getS_CData(@RequestParam int page, HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) throws IOException {
        ResultInfo resultInfo = new ResultInfo();
        PackagedData packagedData = new PackagedDataImpl();
        IPage<S_C_PointEntity> allPointTODataCenter ;

        UserEntity userEntity = (UserEntity) httpServletRequest.getSession().getAttribute("USER");
        List<Permission_TBEntity> integers = getUserPermission.GetPermission(userEntity.getUname());
        if (!integers.isEmpty()) {
            for (Permission_TBEntity permission : integers) {
                DataCenter_permissionEntity dataCenter_permissionEntities = dataCenterService.perIdFindDataCenter_Per(permission.getPermission());
                if (dataCenter_permissionEntities != null){
                    if (dataCenter_permissionEntities.getPermission().equalsIgnoreCase("s6")) {
                        allPointTODataCenter = spot_check_fusionService.findAllPointTODataCenter(page);
                        resultInfo.setFlag(true);
                        resultInfo.setData(allPointTODataCenter);
                        break;
                    }else {
                        resultInfo.setFlag(false);
                        resultInfo.setErrorMsg("您无权访问！");
                    }
                }else {
                    resultInfo.setFlag(false);
                    resultInfo.setErrorMsg("您无权访问！");
                }

            }
        }else {
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg("账号异常");
        }


        packagedData.writeValue(resultInfo, httpServletResponse);
    }

    @RequestMapping("deleteSCData")
    public void deleteSCData(@RequestParam List<String> pointNameArr,@RequestParam List<String> groupIdArr,HttpServletResponse httpServletResponse,HttpServletRequest httpServletRequest) throws IOException {
        List<Integer> groupIds = new ArrayList<>();


        for (String s : groupIdArr) {
            String replace = s.replace("\"", "").replace("[","").replace("]","");
            groupIds.add(CalculationIsType.calculationIsGroupTypeToMySql(CalculationIsType.calculationIsGroupTypeValue(replace)));
        }

        boolean b = dataCenterService.deleteSCData(pointNameArr, groupIds);
        PackagedData packagedData = new PackagedDataImpl();
        packagedData.writeValue(b,httpServletResponse);

    }
    @RequestMapping("dailySCData")
    public void dailySCData(@RequestParam String pointName,@RequestParam int page, HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) throws IOException {
        IPage<S_C_Point_ContentEntity> s_c_point_contentEntities = dataCenterService.dailySCData(pointName,page);
        ResultInfo resultInfo = new ResultInfo();
        PackagedData packagedData = new PackagedDataImpl();
        if (s_c_point_contentEntities.getRecords().isEmpty()){
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg("查询结果为空");
        }else {
            resultInfo.setFlag(true);
            resultInfo.setData(s_c_point_contentEntities);
        }
        packagedData.writeValue(resultInfo,httpServletResponse);
    }

    @RequestMapping("deleteDailySCData")
    public void deleteDailySCData(@RequestParam List<String> dailyPointNameArr,@RequestParam List<String> dailyPointContextArr,HttpServletResponse httpServletResponse,HttpServletRequest httpServletRequest) throws IOException {
        List<String> dailyPointNameS = new ArrayList<>();
        List<String> dailyPointContextS = new ArrayList<>();
        ResultInfo resultInfo = new ResultInfo();
        PackagedData packagedData = new PackagedDataImpl();

        for (String s : dailyPointNameArr) {
            String replace = s.replace("\"", "").replace("[","").replace("]","");
            dailyPointNameS.add(replace);
        }
        for (String s : dailyPointContextArr) {
            String replace = s.replace("\"", "").replace("[","").replace("]","");
            dailyPointContextS.add(replace);
        }
        boolean b = dataCenterService.deleteDailySCData(dailyPointNameS, dailyPointContextS);
        if (b){
            resultInfo.setFlag(true);
            resultInfo.setErrorMsg("删除成功");
        }else {
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg("删除失败");
        }
        packagedData.writeValue(resultInfo,httpServletResponse);
    }

    @RequestMapping("reversSCDataGroup")
    public void reversSCDataGroup(@RequestParam List<String> groupArr,@RequestParam String pointName,HttpServletResponse httpServletResponse,HttpServletRequest httpServletRequest) throws IOException {
        List<Integer> groupS = new ArrayList<>();

        ResultInfo resultInfo = new ResultInfo();
        PackagedData packagedData = new PackagedDataImpl();

        for (String s : groupArr) {
            String replace = s.replace("\"", "").replace("[","").replace("]","");
            groupS.add(CalculationIsType.calculationIsGroupTypeToMySql(CalculationIsType.calculationIsGroupType(replace)));
        }

        boolean b = dataCenterService.reversSCDataGroup(groupS, pointName.replace("\"", "").replace("[","").replace("]",""));
        if (b){
            resultInfo.setFlag(true);
            resultInfo.setErrorMsg("修改成功");
        }else {
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg("修改失败");
        }
        packagedData.writeValue(resultInfo,httpServletResponse);
    }


    // TODO: 2023/2/8 保存修改的信息
    @RequestMapping("dailyEdit")
    public void dailyEdit(@RequestParam String DailyEdit_pointContent, @RequestParam String pointName,@RequestParam String pointContentInput,@RequestParam String scFunInput,@RequestParam String scBenchmarkInput,HttpServletResponse httpServletResponse,HttpServletRequest httpServletRequest) throws IOException {
        boolean b = dataCenterService.dailyEdit(DailyEdit_pointContent, pointName, pointContentInput, scFunInput, scBenchmarkInput);
        ResultInfo resultInfo = new ResultInfo();
        PackagedData packagedData = new PackagedDataImpl();
        if (b){
            resultInfo.setFlag(true);
            resultInfo.setErrorMsg("修改成功！");
        }else {
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg("修改失败！");
        }
        packagedData.writeValue(resultInfo,httpServletResponse);
    }

    @RequestMapping("getS_CDailyData")
    public void getS_CDailyData(@RequestParam int page,HttpServletResponse httpServletResponse ,HttpServletRequest httpServletRequest) throws IOException, ParseException {
        ResultInfo resultInfo = new ResultInfo();
        PackagedData packagedData = new PackagedDataImpl();
        PageInfo pageInfo = new PageInfo();


        UserEntity userEntity = (UserEntity) httpServletRequest.getSession().getAttribute("USER");
        List<Permission_TBEntity> integers = getUserPermission.GetPermission(userEntity.getUname());
        if (!integers.isEmpty()) {
            for (Permission_TBEntity permission : integers) {
                DataCenter_permissionEntity dataCenter_permissionEntities = dataCenterService.perIdFindDataCenter_Per(permission.getPermission());
                if (dataCenter_permissionEntities != null){
                    System.out.println(dataCenter_permissionEntities.getPermission());
                    if (dataCenter_permissionEntities.getPermission().equalsIgnoreCase("s7")) {
                        List<S_C_DatesEntity> allPointTODataCenter = spot_check_fusionService.findAllPointDailyTODataCenter(page);


                        int totalCurrent = spot_check_fusionService.findTotalCurrent();
                        //计算总页数
                        int totalPage = totalCurrent % 10 == 0 ? totalCurrent / 10 : (totalCurrent / 10) + 1 ;
                        pageInfo.setTotalCurrent(totalCurrent);
                        pageInfo.setTotalPage(totalPage);
                        pageInfo.setRecords(allPointTODataCenter);
                        pageInfo.setCurrentPage(page);
                        pageInfo.setPageSize(10);
                        resultInfo.setFlag(true);
                        resultInfo.setData(pageInfo);
                        if (allPointTODataCenter.isEmpty()){
                            page++;
                            getS_CDailyData(page,httpServletResponse,httpServletRequest);
                            totalCurrent = spot_check_fusionService.findTotalCurrent();
                            totalCurrent -= 10;
                            //计算总页数
                            totalPage = totalCurrent % 10 == 0 ? totalCurrent / 10 : (totalCurrent / 10) + 1 ;
                            totalPage -= 1;
                            pageInfo.setTotalCurrent(totalCurrent);
                            pageInfo.setTotalPage(totalPage);
                            pageInfo.setRecords(allPointTODataCenter);
                            pageInfo.setCurrentPage(1);
                            pageInfo.setPageSize(10);
                            resultInfo.setFlag(true);
                            resultInfo.setData(pageInfo);
                        }else {
                            break;
                        }

                    }else {
                        resultInfo.setFlag(false);
                        resultInfo.setErrorMsg("您无权访问！");
                    }
                }else {
                    resultInfo.setFlag(false);
                    resultInfo.setErrorMsg("您无权访问！");
                }

            }
        }else {
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg("账号异常");
        }
        packagedData.writeValue(resultInfo, httpServletResponse);
    }

}
